package com.annotation.hw.hw5.user;

import com.annotation.hw.hw5.animal.Animal;
import com.annotation.hw.hw5.annotation.JianKang;
import com.annotation.hw.hw5.annotation.ShuiMian;

import java.lang.reflect.Method;

/**
 * 饲养员类 - 泛型类(可以饲养任意类型的对象) + 边界（对象一定是动物）
 * @author bin
 * @date 2025 - 03 -31
 **/
public class SiYangYuan<T extends Animal>  {
    public void xunCha(T animal){
        Class<? extends Animal> aClass = animal.getClass();
        try {
            Method sleep = aClass.getDeclaredMethod("sleep");
            if (sleep.isAnnotationPresent(ShuiMian.class)) {
                String value = sleep.getAnnotation(ShuiMian.class).value();
                if("失眠".equals(value)){
                    System.out.println("给你一颗安眠药");
                }
            }

        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }

    public void tiJian(T animal){
        Class<? extends Animal> aClass = animal.getClass();
        try {
            if (aClass.isAnnotationPresent(JianKang.class)) {
                String value = aClass.getAnnotation(JianKang.class).value();
                if("差".equals(value)){
                    System.out.println("需要特别照看！");
                }
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
